{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/home/DISCOVER_summer2022/xusc/exp/MonoDETR'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "\n",
    "os.chdir('/home/DISCOVER_summer2022/xusc/exp/MonoDETR/')\n",
    "os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "dataset_directory = \"data/KITTIDataset/training/image_2\"  # Adjust the path to your images\n",
    "output_file = \"data/KITTIDataset/ImageSets/train.txt\"\n",
    "\n",
    "# Get a list of filenames without file extensions\n",
    "filenames = [os.path.splitext(f)[0] for f in os.listdir(dataset_directory) if f.endswith('.png')]\n",
    "\n",
    "# Write filenames to the output file\n",
    "with open(output_file, 'w') as file:\n",
    "    for filename in filenames:\n",
    "        file.write(f\"{filename}\\n\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<__main__.OfficialKittiPreparor at 0x7f5e4d660370>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from os.path import  join, split, exists, isdir, isfile\n",
    "\n",
    "\n",
    "\n",
    "import numpy as np \n",
    "import glob\n",
    "\n",
    "class KittiPreparor:\n",
    "\n",
    "\n",
    "    def __init__(self, root):\n",
    "\n",
    "        self.src_path = join(root, 'all_sequences')\n",
    "        self.train_seq_names = [ \"{:02d}\".format(x) for x in range(11)]\n",
    "        self.train_seq_names.remove('08')\n",
    "        self.test_seq_names = ['08']\n",
    "\n",
    "\n",
    "        train_images = []\n",
    "        for seq_name in self.train_seq_names:\n",
    "            train_images = train_images + glob.glob( join(self.src_path, seq_name,'image_2/*.png'))\n",
    "        test_images = []\n",
    "        for seq_name in self.test_seq_names:\n",
    "            test_images = train_images + glob.glob( join(self.src_path, seq_name,'image_2/*.png'))\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        self.test_images = test_images\n",
    "        self.train_images = train_images\n",
    "\n",
    "        \n",
    "        np.savetxt(join(root, 'ImageSets','val.txt'), self.test_images, delimiter = '\\n', fmt = '%s')\n",
    "        np.savetxt(join(root, 'ImageSets','train.txt'), self.train_images, delimiter = '\\n', fmt = '%s')\n",
    "        \n",
    "        \n",
    "\n",
    "\n",
    "class OfficialKittiPreparor:\n",
    "\n",
    "\n",
    "    def __init__(self, root):\n",
    "\n",
    "\n",
    "        train_images = glob.glob( join(root, 'training','image_2/*.png'))\n",
    "        train_images = [x.split('/')[-1].split('.')[0] for x in train_images]\n",
    "\n",
    "        \n",
    "        test_images =  glob.glob( join(root, 'testing','image_2/*.png'))\n",
    "        test_images = [x.split('/')[-1].split('.')[0] for x in test_images]\n",
    "\n",
    "        self.test_images = test_images\n",
    "        self.train_images = train_images\n",
    "\n",
    "        \n",
    "        np.savetxt(join(root, 'ImageSets','val.txt'), self.test_images, delimiter = '\\n', fmt = '%s')\n",
    "        np.savetxt(join(root, 'ImageSets','train.txt'), self.train_images, delimiter = '\\n', fmt = '%s')\n",
    "        \n",
    "        \n",
    "        \n",
    "        \n",
    "root = 'data/KITTIDataset'\n",
    "\n",
    "\n",
    "# KittiPreparor(root)\n",
    "OfficialKittiPreparor(root)\n",
    "        \n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['00', '01', '02', '03', '04', '05', '06', '07', '08', '09']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[ \"{:02d}\".format(x) for x in range(11)]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yang_real",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
